home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS20.ADF
/
BobEd
/
events.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-01-27
|
17KB
|
461 lines
#include <bobed.h>
/*
"events.c" - BobEd event processor
These routines are the guts of the editor.
*/
VOID drawx (); /* draw a crosshair for saving object */
VOID drawrbox (); /* draw a rubber box for framing object */
int frame (); /* frame object for saving */
VOID copyview (); /* copy one object to another frame */
VOID rewrite (); /* redraw the object */
VOID clear (); /* clear the current object */
VOID flip (); /* flip the current object on its y axis */
VOID rotate (); /* rotate current object 90 degrees right */
VOID makecolor (); /* make selected color current */
VOID newview (); /* make selected object current */
VOID drawpixel (); /* draw big pixel and small one in current ob */
int clip (); /* determine if mouse is outside edit box */
extern int save (); /* defined in disk.c */
extern int load ();
extern struct Window *w;
extern struct Screen *screen;
extern struct RastPort *rp;
extern SHORT colorbox [];
extern SHORT boxes [2][HBOBS*VBOBS];
extern struct Requester loadreq;
extern struct Requester savereq;
extern struct Requester inforeq;
extern struct Gadget formatgad; /* used to check desired save format */
extern struct StringInfo stringinfo;/* file name */
extern struct StringInfo intinfo; /* number of bit planes to save */
extern UBYTE bobdata [OBS+1][BWIDE*16][BHIGH]; /* image data */
UBYTE touched [OBS]; /* has this image been modified? */
VOID doevent ()
{
USHORT class; /* IDCMP message class */
USHORT code; /* IDCMP message code */
APTR address; /* IDCMP message address */
struct IntuiMessage *m1; /* IDCMP message */
USHORT msx,msy; /* mouse coordinates */
BOOL pendown=FALSE; /* pen status */
UBYTE color=1; /* current drawing color*/
UBYTE id,reqid; /* gadget id code */
UBYTE view=0; /* current image */
UBYTE menunum,itemnum; /* menu number, item num*/
USHORT x0,y0,x1,y1; /* frame size */
UBYTE savestat; /* load/save success */
UBYTE planes=2; /* bit planes to save */
UBYTE ccode=TRUE; /* flag for c format */
makecolor (color); /* fill color selected box with current color */
FOREVER /* repeat until user quits */
{
Wait (1<<w->UserPort->mp_SigBit); /* wait for a message */
{
/*
this is not the most efficient way to get the messages, fast
messages may go unnoticed (MOUSEMOVE in specific).
proper way is to get messages until the queue is empty
and then process the events. V1.0 falls behind if the mouse
is moved quickly.
*/
while ((m1=(struct IntuiMessage *)GetMsg(w->UserPort)))
{
class = m1->Class; /* get the info we need */
code = m1->Code;
address = m1->IAddress;
msx = m1->MouseX;
msy = m1->MouseY;
ReplyMsg (m1); /* and reply right away ! */
switch (class)
{
case MOUSEMOVE:
if (!pendown) break;
if (!clip (msx,msy)) /* if inside draw box then draw */
drawpixel (msx,msy,color,view);
break; /* if ouside draw box then dont do anything */
case MOUSEBUTTONS:
if (code == SELECTDOWN) /* left button pressed */
{
pendown = TRUE; /* put the pen down to draw */
if (!clip (msx,msy)) drawpixel (msx,msy,color,view);
}
else pendown = FALSE; /* must be SELECTUP so dont draw */
break;
case GADGETUP: /* one of many possible gadgets selected */
{
id = ((struct Gadget *)address)->GadgetID; /* get ID */
if (id < 16) /* first 16 are the color boxes */
{
color = id; /* easy color select */
makecolor (color); /* change the color selected box */
break;
} else if (id < REQBASE) { /* is it a view select gad? */
view = id-VIEWBASE; /* sub offset to get view sel */
if (touched[view] == 0) /* has this view been modif? */
clear (view); /* if not, just clear draw box */
else /* if it has then redraw draw box */
newview (view);
break;
} else { /* a requester gadget has been hit */
reqid = id-REQBASE; /* get the ID */
switch (reqid)
{
case 0: /* load requester gadget selected */
if (stringinfo.NumChars > 0) /* no file name? */
{
copyview (view,9); /* save in case of unsucessful load */
clear (view); /* clear array & view */
savestat = load (view,stringinfo.Buffer);
if (savestat == FALSE) copyview (9,view); /*restore old view */
else touched[view] = TRUE;
rewrite (view); /* draw into small view */
newview (view); /* draw into draw box */
}
break;
case 3: /* save requester gadget selected */
if (stringinfo.NumChars > 0) /* no file name?*/
{
if ((formatgad.Flags & SELECTED) > 0) /* save as C code? */
{
ccode = TRUE;
} else
ccode = FALSE;
savestat = frame (view,&x0,&y0,&x1,&y1); /*draw frame around desired image */
planes = (SHORT)intinfo.LongInt; /* number ofplanes */
if (savestat == TRUE) /* user did not cancel*/
savestat = save (ccode,stringinfo.Buffer,view,x0,y0,x1,y1,planes);
SetDrMd (rp, JAM1);
pendown=FALSE;
}
break;
default: /* cancel */
break;
}
break;
}
}
case MENUPICK: /* user pressed right button */
{
if (code != MENUNULL) /* if button not pressed on the menu
bar, code will=MENUNULL */
{
menunum = MENUNUM (code); /* which menu */
itemnum = ITEMNUM (code); /* which item */
switch (menunum)
{
case 0: /* file menu selected */
switch (itemnum)
{
case 0: /* user wants info */
Request (&inforeq,w);
break;
case 1: /* user wants to load */
Request (&loadreq,w);
break;
case 2: /* user wants to save */
Request (&savereq,w);
break;
case 3: /* user wants to quit */
bye (NULL);
default:
break;
}
break;
case 1:
switch (itemnum)
{
case 0: /* clear the present view */
clear (view);
break;
case 1: /* flip the present view */
flip (view);
rewrite (view);
newview (view);
break;
case 2: /* rotate the present view */
rotate (view);
rewrite (view);
newview (view);
break;
default: /* ignore anything else */
break;
}
break;
case 2:
switch (itemnum)
{
case 0: /* take a snapshot of this view */
copyview (view,9);
break;
case 1: /* copy the snapshot */
touched[view]=TRUE;
copyview (9,view);
rewrite (view);
newview (view);
break;
}
default:
break;
}
}
}
}
}
}
}
}
VOID rotate (v) /* rotate the view 90 deg. to the right */
SHORT v;
{
REGISTER int x,y;
for (y=0;y<BWIDE*16;y++)
for (x=0;x<BWIDE*16;x++)
bobdata [9][BWIDE*16-y-1][x] = bobdata [v][x][y];
copyview (9,v);
}
VOID copyview (s,t)
SHORT s,t;
{
REGISTER int x,y;
for (y=0;y<BHIGH;y++)
for (x=0;x<BWIDE*16;x++)
bobdata [t][x][y] = bobdata [s][x][y];
}
VOID flip (v) /* flip this view */
SHORT v;
{
REGISTER int x,y,c;
for (y=0;y<BHIGH;y++)
for (x=0;x<(BWIDE*8);x++)
{
c = bobdata [v][x][y];
bobdata [v][x][y] = bobdata [v][(BWIDE*16)-x-1][y];
bobdata [v][(BWIDE*16)-x-1][y] = c;
}
}
VOID rewrite (v) /* redraw the small view */
SHORT v;
{
REGISTER int x,y;
int c;
for (y=0;y<BHIGH;y++)
for (x=0;x<BWIDE*16;x++)
{
SetAPen (rp, bobdata [v][x][y]); /* the data contains the color */
c=WritePixel (rp, boxes [0][v]+x,boxes [1][v]+y);
}
}
VOID clear (v) /* clear the data and blank both views */
SHORT v;
{
REGISTER int x,y;
touched[v]=0;
for (y=0;y<BHIGH;y++)
for (x=0;x<(BWIDE*16);x++)
bobdata [v][x][y] = 0;
SetAPen (rp,0); /* fill both boxes with background colors */
RectFill (rp, LOFFSET,TOFFSET,HWIDE-ROFFSET-1,HIGH-BOFFSET-1);
RectFill (rp,boxes[0][v],boxes[1][v],boxes[0][v]+BWIDE*16-1,boxes[1][v]+BHIGH-1);
}
int clip (x,y) /* check to see if user wants to draw outside the bigbox*/
int x,y;
{
if ((x > LOFFSET+1) && (x < (HWIDE-ROFFSET-1)) && (y < (HIGH-BOFFSET-1)) && (y> TOFFSET+1))
return (FALSE); /* it is ok */
return (TRUE); /* it is outside boundary */
}
VOID drawpixel (x,y,c,v) /* draw one dot in the little view and the bigbox*/
int x,y,c,v;
{
int xx,yy;
int xind,yind;
xind = (x-LOFFSET-1)/HBLOCK;
yind = (y-TOFFSET-1)/VBLOCK;
xx = LOFFSET+1+xind*HBLOCK;
yy = TOFFSET+1+yind*VBLOCK;
touched[v]=1;
SetAPen (rp, c);
RectFill (rp, xx,yy,(xx+HBLOCK-1),(yy+VBLOCK-1));
xx=WritePixel (rp, boxes [0][v]+xind,boxes [1][v]+yind);
bobdata [v][xind][yind] = (UBYTE)c;
}
VOID makecolor (c) /* fill the color selected box with the current color */
SHORT c;
{
SetAPen (rp, c);
RectFill (rp, colorbox [0]+1,colorbox [1]+1,colorbox [4]-1,colorbox [5]-1);
}
VOID newview (v) /* redraw the data into the big draw box */
SHORT v;
{
REGISTER int xx,yy;
int x,y,lo,to,bw;
lo = LOFFSET+1;
to = TOFFSET+1;
bw = BWIDE*16;
for (yy=0;yy<BHIGH;yy++)
{
y = to+yy*VBLOCK;
for (xx=0;xx<bw;xx++)
{
x = lo+xx*HBLOCK;
SetAPen (rp, bobdata [v][xx][yy]);
RectFill (rp, x,y,x+HBLOCK-1,y+VBLOCK-1); /* draw a fat pixel */
}
}
}
int frame (v,x0,y0,x1,y1) /* frame the portion of the view
you want to save */
SHORT v,*x0,*y0,*x1,*y1;
{
USHORT class; /* we get our own events in this routine */
USHORT code; /* see doevent for explanation of these */
struct IntuiMessage *m1;
int msx,msy,t;
int oldx=0,oldy=0; /* save previous mouse position */
int rubberbox=FALSE;
FOREVER
{
Wait (1<<w->UserPort->mp_SigBit);
{
while ((m1=(struct IntuiMessage *)GetMsg(w->UserPort)))
{
class = m1->Class;
code = m1->Code;
msx = m1->MouseX;
msy = m1->MouseY;
ReplyMsg (m1);
switch (class)
{
case MOUSEMOVE:
if (clip (msx,msy))
{
if (msx < LOFFSET+1) msx = LOFFSET+1;
if (msy < TOFFSET+1) msy = TOFFSET+1;
if (msx > HWIDE-ROFFSET-1) msx = HWIDE-ROFFSET-1;
if (msy > HIGH-BOFFSET-1) msy = HIGH-BOFFSET-1;
}
if (rubberbox == TRUE)
{
drawrbox (*x0,*y0,*x1,*y1);
msx=(msx-LOFFSET-1)/HBLOCK;
msy=(msy-TOFFSET-1)/VBLOCK;
*x1=msx;*y1=msy;
drawrbox (*x0,*y0,*x1,*y1);
break;
} else {
if (oldx != 0)
drawx (oldx,oldy);
oldx = msx;
oldy = msy;
drawx (msx,msy);
}
break;
case MOUSEBUTTONS:
switch (code)
{
case SELECTUP:
if (rubberbox == TRUE)
{
drawrbox (*x0,*y0,*x1,*y1);
if ((*x0 == *x1) || (*y0 == *y1)) return (FALSE);
if (*x0 > *x1) { t=*x1; *x1=*x0; *x0=t; }
if (*y0 > *y1) { t=*y1; *y1=*y0; *y0=t; }
return (TRUE);
}
else
{
if (rubberbox == FALSE)
{
if (clip (msx,msy))
{
if (msx < LOFFSET+1) msx = LOFFSET+1;
if (msy < TOFFSET+1) msy = TOFFSET+1;
if (msx > HWIDE-ROFFSET-1) msx = HWIDE-ROFFSET-1;
if (msy > HIGH-BOFFSET-1) msy = HIGH-BOFFSET-1;
}
drawx (oldx,oldy);
rubberbox=TRUE;
msx=(msx-LOFFSET-1)/HBLOCK;
msy=(msy-TOFFSET-1)/VBLOCK;
*x0=msx; *x1=msx;
*y0=msy; *y1=msy;
drawrbox (*x0,*y0,*x1,*y1);
break;
}
}
}
default:
break;
}
}
}
}
}
VOID drawrbox (x0,y0,x1,y1)
SHORT x0,y0,x1,y1;
{
x0 = LOFFSET+1+x0*HBLOCK-1;
y0 = TOFFSET+1+y0*VBLOCK-1;
x1 = LOFFSET+1+x1*HBLOCK;
y1 = TOFFSET+1+y1*VBLOCK;
SetAPen (rp,1);
SetDrMd (rp, COMPLEMENT);
Move (rp, x0,y0);
Draw (rp, x1,y0);
Draw (rp, x1,y1);
Draw (rp, x0,y1);
Draw (rp, x0,y0);
}
VOID drawx (x,y)
SHORT x,y;
{
SetAPen (rp,1);
x = (x-LOFFSET-1)/HBLOCK;
x = LOFFSET+1+x*HBLOCK-1;
y = (y-TOFFSET-1)/VBLOCK;
y = TOFFSET+1+y*VBLOCK-1;
SetDrMd (rp, COMPLEMENT);
Move (rp, x,TOFFSET+1);
Draw (rp, x,HIGH-BOFFSET-1);
Move (rp, LOFFSET+1,y);
Draw (rp, HWIDE-ROFFSET-1,y);
}